package com.example.thread.delay;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.DelayQueue;

public class CalculatenumberTester {
    public static void main(String[] args) throws IOException {
        DelayQueue<Calculatenumber> d = new DelayQueue<Calculatenumber>();
        //开一个线程作为生产者
        new Thread(() -> {
            LocalDateTime localDateTime = LocalDateTime.now();
            DateTimeFormatter f1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            System.out.println(localDateTime.format(f1));
            System.out.println("开始放入1");
            d.put(new Calculatenumber(3000, 1));
            System.out.println("开始放入2");
            d.put(new Calculatenumber(2000, 2));
            System.out.println("开始放入3");
            d.put(new Calculatenumber(1000, 3));
            System.out.println("开始放入4");
            d.put(new Calculatenumber(5000, 4));
            System.out.println("开始放入5");
            d.put(new Calculatenumber(500, 5));
        }).start();

        //开一个线程作为消费者
        new Thread(() -> {
            try {
                while (true) {
                    DateTimeFormatter f1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    System.out.println("开始消费" + d.take().getDate());
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        // 开启一个计时调度，延迟 0毫秒（也就是立即开始执行），每秒输出当前时间
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                LocalDateTime localDateTime = LocalDateTime.now();
                DateTimeFormatter f1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                System.out.println(localDateTime.format(f1));
            }
        }, 0L, 1000L);
    }

}
